#include "songinfodialog.h"

#include <QDialog>
#include <QWidget>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QPixmap>
#include <QFrame>

#include <QDebug>

#include "../Types/song.h"

#include "utils.h"
#include "../globals.h"

SongInfoDialog :: SongInfoDialog(const Song& s, QWidget* parent/*=0*/)
    :QDialog(parent)
{
    unsigned int artistCounter = 0;

    this->setWindowTitle(tr("NUP : Song %1 viewer").arg(s.id));
    this->setWindowIcon(QIcon(ICONSDIR + QString("music.png")));

    pHLine1 = new QFrame(this);
    pHLine1->setFrameStyle(QFrame::HLine);
    pHLine1->setLineWidth(1);

    pMainGrid = new QVBoxLayout(this);

    if ( s.platform != "" )
    {
        pTitleLabel = new QLabel(QString('(') + s.platform + QString(") ") + QString("<b>") + QString(s.title) + QString("</b>"));
    }
    else
    {
        pTitleLabel = new QLabel(QString("<b>") + QString(s.title) + QString("</b>"));
    }

    pArtistsGrid = new QGridLayout();
    pArtistsGrid->addWidget(new QLabel(tr("by:")),0,0);

    for ( QList<Artist>::const_iterator itArtist = s.artists.begin() ; itArtist != s.artists.end() ; itArtist++ , artistCounter++ )
    {
        QLabel* pArtistFlag= new QLabel(this);
        QLabel* pArtistLabel = new QLabel(itArtist->nickname,this);

        pArtistFlag->setPixmap(QPixmap(Utils::getFlagPathFromName(itArtist->flag)));

        pArtistsGrid->addWidget(pArtistFlag,artistCounter,1);
        pArtistsGrid->addWidget(pArtistLabel,artistCounter,2);
    }

    if ( s.user->name != "" )
    {
        pUploaderFlagLabel = new QLabel(this);
        pUploaderNameLabel = new QLabel(s.user->name,this);
        pUploaderFlagLabel->setPixmap(QPixmap(Utils::getFlagPathFromName(s.user->flag)));

        pArtistsGrid->addWidget(new QLabel(tr("Uploader:")),artistCounter,0);
        pArtistsGrid->addWidget(pUploaderFlagLabel,artistCounter,1);
        pArtistsGrid->addWidget(pUploaderNameLabel,artistCounter,2);
        artistCounter++;
    }

    pSongInfoLayout = new QVBoxLayout();
    pSongInfoLabel = new QLabel(tr("<b>Song info</b>"),this);
    pSongNoteLabel = new QLabel(tr("Note: %1 (%2 Voters)").arg(s.actualNote).arg(s.nbVotes),this);
    pSongLengthLabel = new QLabel(tr("Length: ") + s.length.toString("m:ss"),this);
    pSongStatusLabel = new QLabel(tr("Status: ") + statusToString(s.status),this);
    if ( s.isLocked )
    {
        pSongLockLabel = new QLabel(QString("<b>") + QString(tr("Song Locked ")) + QString("</b>") + QString(tr("until ")) + QString("<b>") + s.endLockDate.toString("hh:mm:ss ddd dd MMMM yyyy") + QString("</b>"));
    }
    else
    {
        // ToDo : Request button
        pSongLockLabel = NULL;
    }
    pSongTypeLabel = new QLabel(QString(tr("Type: ")) + s.type,this);
    pSongBitrateLabel = new QLabel(tr("Bitrate: %1 bps").arg(s.bitrate),this);
    pSongSampleRateLabel = new QLabel(tr("Sample rate: %1Hz").arg(s.sampleRate),this);
    if ( !s.info.isEmpty() )
    {
        pSongAdditionnalInfoViewer = new QTextEdit(this);
        {
            QString infoString = s.info;
            infoString.prepend("<b>Additionnal Info:</b><br/>");

            pSongAdditionnalInfoViewer->setHtml(infoString);
        }
    }
    else
    {
        pSongAdditionnalInfoViewer = NULL;
    }
    pSongInfoLayout->addWidget(pSongInfoLabel,0,Qt::AlignHCenter);
    pSongInfoLayout->addWidget(pSongNoteLabel,0,Qt::AlignHCenter);
    pSongInfoLayout->addWidget(pSongLengthLabel,0,Qt::AlignHCenter);
    pSongInfoLayout->addWidget(pSongStatusLabel,0,Qt::AlignHCenter);
    if ( pSongLockLabel != NULL )
    {
        pSongInfoLayout->addWidget(pSongLockLabel,0,Qt::AlignHCenter);
    }
    pSongInfoLayout->addWidget(pSongTypeLabel,0,Qt::AlignHCenter);
    pSongInfoLayout->addWidget(pSongBitrateLabel,0,Qt::AlignHCenter);
    pSongInfoLayout->addWidget(pSongSampleRateLabel,0,Qt::AlignHCenter);
    if ( pSongAdditionnalInfoViewer != NULL )
    {
        pSongInfoLayout->addWidget(pSongAdditionnalInfoViewer,0,Qt::AlignHCenter);
    }

    pValidateButton = new QPushButton(tr("Ok"),this);

    if ( s.pouetId )
    {
        pHLine2 = new QFrame(this);
        pHLine2->setFrameStyle(QFrame::HLine);
        pHLine2->setLineWidth(1);

        pAdditionnalLayout = new QVBoxLayout();
        pAdditionnalLabel = new QLabel(tr("<b>Additionnal links</b>"),this);
        pAdditionnalLayout->addWidget(pAdditionnalLabel,0,Qt::AlignHCenter);

        pPouetLabel = new QLabel(QString("<a href=\"http://www.pouet.net/prod.php?which=%1\">Pouet Link</a>").arg(s.pouetId),this);
        pPouetLabel->setOpenExternalLinks(true);
        pAdditionnalLayout->addWidget(pPouetLabel,0,Qt::AlignHCenter);
    }

    pMainGrid->addWidget(pTitleLabel,0,Qt::AlignHCenter);
    pMainGrid->addSpacing(20);
    pMainGrid->addLayout(pArtistsGrid);
    pMainGrid->addWidget(pHLine1);
    pMainGrid->addLayout(pSongInfoLayout);
    if ( s.pouetId )
    {
        pMainGrid->addWidget(pHLine2);
        pMainGrid->addLayout(pAdditionnalLayout);
    }
    pMainGrid->addWidget(pValidateButton);

    connect(pValidateButton, SIGNAL(clicked()), this, SLOT(accept()));

    qDebug() << "SongInfoDialog created";
}

SongInfoDialog :: ~SongInfoDialog(void)
{
    qDebug() << "SongInfoDialog deleted";
}

void SongInfoDialog :: accept(void)
{
    qDebug() << "SongInfoDialog :: accept";

    this->done(QDialog::Accepted);
    emit deleteSIDSignal(this);
}

void SongInfoDialog :: reject(void)
{
    qDebug() << "SongInfoDialog :: reject";

    this->done(QDialog::Rejected);
    emit deleteSIDSignal(this);
}
